home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-30  |  2.1 KB  |  106 lines

  1. #include "sysincludes.h"
  2. #include "msdos.h"
  3. #include "vfat.h"
  4.  
  5. /*
  6.  * Get name component of filename.  Returns pointer to static area.
  7.  *
  8.  * Formerly translated name to upper case; now preserves case.
  9.  * This is because long DOS names are case sensitive, but also because we
  10.  * want to preserve the user-specified case for the copied Unix files
  11.  */
  12.  
  13. char *get_name(char *filename, char *ans, char *mcwd)
  14. {
  15.     char *s, *temp;
  16.  
  17.     temp = filename;
  18.                     /* skip drive letter */
  19.     if (temp[0] && temp[1] == ':')
  20.         temp = &temp[2];
  21.                     /* find the last separator */
  22.     if ((s = strrchr(temp, '/')))
  23.         temp = s + 1;
  24.     if ((s = strrchr(temp, '\\')))
  25.         temp = s + 1;
  26.  
  27.     strncpy(ans, temp, VBUFSIZE-1);
  28.     ans[VBUFSIZE-1]='\0';
  29.  
  30.     return ans;
  31. }
  32.  
  33. /*
  34.  * Get the path component of the filename.
  35.  * Doesn't alter leading separator, always strips trailing separator (unless
  36.  * it is the path itself).
  37.  *
  38.  * Formerly translated name to upper case; now preserves case.
  39.  * This is because long DOS names are case sensitive, but also because we
  40.  * want to preserve the user-specified case for the copied Unix files
  41.  */
  42.  
  43. char *get_path(char *filename, char *ans, char *mcwd)
  44. {
  45.     char *s, *end, *begin;
  46.     char drive;
  47.     int has_sep;
  48.  
  49.     begin = filename;
  50.  
  51.     /* skip drive letter */
  52.     drive = '\0';
  53.     if (begin[0] && begin[1] == ':') {
  54.         drive = toupper(begin[0]);
  55.         begin += 2;
  56.     }
  57.  
  58.     /* if absolute path */
  59.     if (*begin == '/' || *begin == '\\')
  60.         ans[0] = '\0';
  61.     else {
  62.         if (!drive || drive == *mcwd)
  63.             strcpy(ans, mcwd + 2);
  64.         else
  65.             strcpy(ans, "/");
  66.     }
  67.  
  68.     /* find last separator */
  69.     has_sep = 0;
  70.     end = begin;
  71.     if ((s = strrchr(end, '/'))) {
  72.         has_sep++;
  73.         end = s;
  74.     }
  75.     if ((s = strrchr(end, '\\'))) {
  76.         has_sep++;
  77.         end = s;
  78.     }
  79.  
  80.     if(strlen(ans)+end-begin+1 > MAX_PATH){
  81.         fprintf(stderr,"Path too long");
  82.         cleanup_and_exit(1);
  83.     }
  84.  
  85.     strncat(ans, begin, end - begin);
  86.     ans[strlen(ans)+end-begin] = '\0';
  87.  
  88.     /* if separator alone, put it back */
  89.     if (begin == end && has_sep)
  90.         strcat(ans, "/");
  91.  
  92.     return ans;
  93. }
  94.  
  95. /*
  96.  * get the drive letter designation
  97.  */
  98.  
  99. char get_drive(char *filename, char def)
  100. {
  101.     if (*filename && *(filename + 1) == ':')
  102.         return(toupper(*filename));
  103.     else
  104.         return def;
  105. }
  106.